<?php
/*   Description  of this file
*    Author king-of-war
*    Last Modified 2009-5-4
*    This class designed  for the flink operation
*/
class flink {
	private $url;
	function __construct($url) {
		/*
		 * init the $url as rootpath
		 *
		 */
		include_once $url . 'includes/conn.php';
		$this->set_url($url);
	}
	/*
	 *   add & update frend_link
	 *
	 * $fun=1 means add
	 * $fun=2 means update
	 *
	 */
	function flink_add($id, $name, $image, $introduction, $url, $verify, $fun) {
			$conn = new conn();
		 $error = 'null';
		 if (!empty ($fun)) {

			if ($fun == '1') {
				/*
				 * add news sql
				 *
				 */
				$sql = sprintf("INSERT INTO flinks(id,name,images,introduction,url,verify)VALUES(null,'%s','%s','%s','%s','%d')", $name, $image, $introduction, $url, $verify);
				$error = 'add friend link  error';
			}
			elseif ($fun == '2' && !empty ($id)) {
				/*
				* update news sql
				*/
				$sql = sprintf("update flinks set name='%s',images='%s',introduction='%s',url='%s',verify='%d' where id='%s'", $name, $image, $introduction, $url, $verify,$id);

				$error = 'update friend link error';

			}

			$conn->set_sql($sql);

			$line = $conn->get_line();

			return $line;

		} else {

			return $line = 0;

			echo $error;
		}


	}

	/*
	 * friend link read
	 *
	 * $id is the id of the data which you need
	 *
	 * if $id is array , the function will get result as array
	 *
	 * $fun means admin   select with all or limit
	 * @return array
	 */
	function flink_get($id, $fun) {
		$conn = new conn();
		$url = $this->get_url();
		include_once $url . 'includes/entity/flinks.entity.php';
		$info = '';
		if (!$fun) {
			$info = ' where verify =1 ';
		}
		if (is_array($id)) {
			/*
			 * return the resluts as array
			 * $a means start   $b means counts
			 */
			$a = $id[0];
			$b = $id[1];
			$sql = sprintf("select * from flinks " . $info . "limit %d,%d ", $a, $b);
		} else {
			/*
			 *  reslut is only one
			 */
			$sql = sprintf("select * from flinks where id='%d' " . $info, $id);
		}

		$conn->set_sql($sql);
		$line = $conn->get_line();
		if ($line) {
			$result = $conn->get_result();
			for ($i = 0; $row = $result->fetch_assoc(); $i++) {
				$flinks[$i] = new flinks_entity();
				$id = $row['id'];
				$name = $row['name'];
				$image = $row['images'];
				$introduction = $row['introduction'];
				$url = $row['url'];
				$verify = $row['verify'];
				$flinks[$i]->set_id($id);
				$flinks[$i]->set_name($name);
				$flinks[$i]->set_images($image);
				$flinks[$i]->set_introduction($introduction);
				$flinks[$i]->set_url($url);
				$flinks[$i]->set_verify($verify);
			}
		} else {
			echo 'result null';
			$news = null;
		}
		return $flinks;
	}
	/*
	 *  function Delete flinks
	 */
	function flink_del($ids) {
		if (isset ($ids)) {
			if (is_array($ids)) {
				$max = count($ids);
				for ($i = 0; $i < $max; $i++) {
					$conn = new conn();
					$sql = sprintf("delete from flinks where id=%d ; ", $ids[$i]);
					$conn->set_sql($sql);
					$line1 = $conn->get_line();
					$line += $line1;
				}
			} else {
				$conn = new conn();
				$sql = sprintf("delete from flinks where id=%d", $ids);
				$conn->set_sql($sql);
				$line = $conn->get_line();
			}
		} else {
			echo 'at least choose one for process please';
			$line = 0;
		}
		return $line;
	}
	/*
		* common function
		*/
	function set_url($url) {
		$this->url = $url;
	}
	function get_url() {
		return $this->url;
	}
}
?>